
from time import sleep

from openai import OpenAI
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor

import json
import threading
from datetime import datetime

import requests



executor = ThreadPoolExecutor(max_workers=10000)
client = OpenAI(
    base_url="http://192.168.80.35:8000/v1",
    api_key="token-abc123",
)

def answer(prompt):
    completion = client.chat.completions.create(
        model="/home/linweibin/liujian/model/Qwen2.5-72B-Instruct-GPTQ-Int8",
        #model="/home/linweibin/liujian/model/Qwen2-72B-Instruct-GPTQ-Int8",
        # model="/home/zhengzhenzhuang/liujian/model/Qwen2-7B-Instruction",
        messages=[
            {"role": "user", "content": prompt}
        ],
        temperature=0,

        # stream=True
    )
    return completion.choices[0].message.content


def q_a2(question, filepath):
    t1 = datetime.now()
    question = "请用原文回答" + question
    prompt_template = """我给你一个知识文本,以及一个相关的问题,请你根据知识文本的原文回答我的问题,原文的内容要全部输出来，不能够省略，不能总结。如果遇到表格的内容，按照每一点原文罗列出来。如果你无法搜寻到问题的答案,只需要告诉我你不知道答案就行.
                 问题为:{question},
                 知识文本为:{context}
                 """

    doc_content = ""
    with open(filepath, 'r', encoding='utf-8') as file:
        # 读取所有行到一个列表中
        lines = file.readlines()
        for line in lines:
            doc_content = doc_content + line
    test = doc_content.replace(" ", "").replace("+","").replace("=","").replace("-","")
    #print(doc_content)

    print(f"原文本长度：{len(doc_content)}")
    print(f"去除空格文本长度：{len(test)}")
    #62146 正常
    #doc_content = doc_content[:62130]+"|   |   |                      | 圾及时清理"
    #doc_content = doc_content[:62130]+"|   |   |                      | 圾及时清理"
    #doc_content = doc_content[:63000]
    #doc_content = doc_content[:62146]+" "


    if len(doc_content) > 100000:
        print("============文件超过130000,截取前130000==============")
        doc_content = doc_content[:100000]

    prompt_text = prompt_template.format(context=doc_content, question=question)
    # print(prompt_text)
    result = "无相关内容"
    result = answer(prompt_text[:1500])
    #sleep(2)
    #print(result)
    t2 = datetime.now()
    second = (t2 - t1).total_seconds()
    #print(f"耗时{second}")
    return result


path_0 = "D:\\ZZZ\\python_workspace\\python-model-code\\qwen\\test.txt"
path_9 = "C:\\Users\\user\\Desktop\\测试\\9.txt"
path_17 = "C:\\Users\\user\\Desktop\\测试\\17.txt"

#test = q_a2("项目编号是什么", path_0)
#print()

#test = q_a2("项目编号是什么", path_9)
#print()

#test = q_a2("项目编号是什么", path_0)
"""

t1 = datetime.now()
future1 = executor.submit(q_a2, "项目编号是什么",path_0)
future2 = executor.submit(q_a2, "项目编号是什么",path_0)
future3 = executor.submit(q_a2, "项目编号是什么",path_0)
future4 = executor.submit(q_a2, "项目编号是什么",path_0)
future5 = executor.submit(q_a2, "项目编号是什么",path_0)

# 获取任务执行结果
print(future1.result())
second = (datetime.now()-t1).total_seconds()
print(f"耗时{second}")
print(future2.result())
second = (datetime.now()-t1).total_seconds()
print(f"耗时{second}")
print(future3.result())
second = (datetime.now()-t1).total_seconds()
print(f"耗时{second}")
print(future4.result())
second = (datetime.now()-t1).total_seconds()
print(f"耗时{second}")
print(future5.result())
second = (datetime.now()-t1).total_seconds()
print(f"耗时{second}")


"""
s=datetime.now()
threads = []

for i in range(2):
    future = executor.submit(q_a2, "项目编号是什么",path_0)
    threads.append(future)

for i in threads:
    print(i.result())

second = (datetime.now()-s).total_seconds()
print(f"耗时{second}")